package com.example.shop.common.filter;

import com.example.shop.common.enums.SessionAttributeKey;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
@WebFilter(filterName = "IpSetFilter",urlPatterns = "/*")
public class IpSetFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) request;
        HttpSession session = req.getSession(true);
        //设置客户端ip:端口号
        session.setAttribute(SessionAttributeKey.CLIENT_IP.getKey(), request.getRemoteAddr());
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {
    }


}
